﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IBaklogTaskService))]
    internal class BaklogTaskService : IBaklogTaskService
    {
        [Import]
        protected IBaklogTaskRepository BaklogTaskRepository { get; set; }

        public List<BaklogTaskView> GetBaklogTaskByProcessPerson(string processPerson)
        {
            List<BaklogTaskView> baklogTaskViewList = new List<BaklogTaskView>();
            IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(b => b.CurrentProcessPerson == processPerson);
            foreach (BaklogTask baklogTask in baklogTasks)
            {
                baklogTaskViewList.Add(new BaklogTaskView()
                {
                    ID = baklogTask.ID,
                    RecordID = baklogTask.RecordID,
                    TaskType = baklogTask.TaskType,
                    RequestDate=baklogTask.RequestDate,
                    CurrentProcessPersonDepartment=baklogTask.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = baklogTask.CurrentProcessPerson,
                    CurrentFlowNodeCode = baklogTask.CurrentFlowNodeCode,
                    NodeLevel=baklogTask.NodeLevel
                });
            }
            return baklogTaskViewList;
        }

        public List<BaklogTaskView> GetBaklogTaskByRecordID(string recordID)
        {
            List<BaklogTaskView> baklogTaskViewList = new List<BaklogTaskView>();
            IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(b => b.RecordID == recordID);
            foreach (BaklogTask baklogTask in baklogTasks)
            {
                baklogTaskViewList.Add(new BaklogTaskView()
                {
                    ID = baklogTask.ID,
                    RecordID = baklogTask.RecordID,
                    TaskType = baklogTask.TaskType,
                    RequestDate = baklogTask.RequestDate,
                    CurrentProcessPersonDepartment = baklogTask.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = baklogTask.CurrentProcessPerson,
                    CurrentFlowNodeCode = baklogTask.CurrentFlowNodeCode,
                    NodeLevel = baklogTask.NodeLevel
                });
            }
            return baklogTaskViewList;
        }

        public List<BaklogTaskView> GetBaklogTask(string processPerson, string recordID)
        {
            List<BaklogTaskView> baklogTaskViewList = new List<BaklogTaskView>();
            IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(b => b.CurrentProcessPerson == processPerson && b.RecordID == recordID);
            foreach (BaklogTask baklogTask in baklogTasks)
            {
                baklogTaskViewList.Add(new BaklogTaskView()
                {
                    ID = baklogTask.ID,
                    RecordID = baklogTask.RecordID,
                    TaskType = baklogTask.TaskType,
                    RequestDate = baklogTask.RequestDate,
                    CurrentProcessPersonDepartment = baklogTask.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = baklogTask.CurrentProcessPerson,
                    CurrentFlowNodeCode = baklogTask.CurrentFlowNodeCode,
                    NodeLevel = baklogTask.NodeLevel
                });
            }
            return baklogTaskViewList;
        }

        public List<BaklogTaskView> GetBaklogTaskByNodeCode(string currentFlowNodeCode, string recordID)
        {
            List<BaklogTaskView> baklogTaskViewList = new List<BaklogTaskView>();
            IQueryable<BaklogTask> baklogTasks = BaklogTaskRepository.Entities.Where(b => b.CurrentFlowNodeCode == currentFlowNodeCode && b.RecordID == recordID);
            foreach (BaklogTask baklogTask in baklogTasks)
            {
                baklogTaskViewList.Add(new BaklogTaskView()
                {
                    ID = baklogTask.ID,
                    RecordID = baklogTask.RecordID,
                    TaskType = baklogTask.TaskType,
                    RequestDate = baklogTask.RequestDate,
                    CurrentProcessPersonDepartment = baklogTask.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = baklogTask.CurrentProcessPerson,
                    CurrentFlowNodeCode = baklogTask.CurrentFlowNodeCode,
                    NodeLevel = baklogTask.NodeLevel
                });
            }
            return baklogTaskViewList;
        }

        public void AddBaklogTask(List<BaklogTaskView> addList)
        {
            foreach (BaklogTaskView baklogTaskView in addList)
            {
                BaklogTask baklogTask = new BaklogTask()
                {
                    ID = baklogTaskView.ID,
                    RecordID = baklogTaskView.RecordID,
                    TaskType = baklogTaskView.TaskType,
                    RequestDate = baklogTaskView.RequestDate,
                    CurrentProcessPersonDepartment=baklogTaskView.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = baklogTaskView.CurrentProcessPerson,
                    CurrentFlowNodeCode = baklogTaskView.CurrentFlowNodeCode,
                    NodeLevel = baklogTaskView.NodeLevel
                };
                BaklogTaskRepository.Insert(baklogTask);
            }
        }

        public void DeleteBaklogTask(List<BaklogTaskView> deleteList)
        {
            foreach (BaklogTaskView baklogTaskView in deleteList)
            {
                BaklogTaskRepository.Delete(baklogTaskView.ID);
            }
        }
    }
}